package com.hongxun.design.service;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.ui.dialogstyle.MyDialog;
import com.hongxun.design.ui.dialogstyle.ProgressLoadingDialog;

/**
 * Created by zerdoor_pc on 2016/9/1.
 */
public class CacheDialog {

    private Handler handler;
    private int times = 0;
    static CacheDialog instence;
    public static CacheDialog getInstence() {
        if (instence == null) {
            instence = new CacheDialog(MyApp.activity);
        }
        return instence;
    }

    CacheDialog(Context context) {
        times = 0;
        addHandler();
        startDialog(context);
    }

    private void addHandler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 3:
                        MyLog.L(MString.SUCCESSED+"Message "+3+"times="+times);
                        if (dialog!=null&&dialog.isShowing()){
                            ToastShow.showShort(MyApp.activity,"缓存失败！");
                            MyLog.L(MString.SUCCESSED + "times=" + times);
                            close();
                        }
                        break;
                    default:
                        times += 1;
                        if (times >=8) {
                            close();
                        }
                        MyLog.L(MString.SUCCESSED+"times="+times);
                        break;
                }

            }
        };
    }

    public Handler getHandler() {
        return handler;
    }

    private void close() {
        closeDialog();
        instence = null;
    }


    private void closeDialog() {
        try {
           if (dialog!=null)dialog.dismiss();
        }catch (Exception e){}
    }

    ProgressLoadingDialog dialog;

    private void startDialog(Context context) {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                handler.sendEmptyMessage(3);
            }
        },10000);

        if (context == null) return;
        try {
            if (dialog == null) {
                try {

                    DialogInterface.OnKeyListener keylistener = new DialogInterface.OnKeyListener() {
                        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                            return keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0;
                        }
                    };

                    dialog = new ProgressLoadingDialog(context, MString.Loading, R.mipmap.load_image);
                   // dialog.setOnKeyListener(keylistener);
                    dialog.setCancelable(false);
                    dialog.show();
                } catch (Exception e) {
                }
            } else {
                if (!dialog.isShowing()) dialog.show();
            }
        } catch (Exception e) {
            MyLog.L(MString.EXCEPTION + ":" + e.toString());
        }

    }
}
